﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

namespace MyHealthByDesign.Domain.Panels
{
    public class CheckBoxList : IQuestionPanel
    {
        #region IQuestionPanel Members

        public System.Web.UI.WebControls.Panel GenerateQuestionDisplay(Question q)
        {
            Panel pnlIn = new Panel();

            Panel pnlTopic = new Panel();
            
            Label lblC = new Label();
            lblC.Text = q.ContentLabel;
            lblC.CssClass = "panelLeft";

            pnlTopic.Controls.Add(lblC);

            if (!string.IsNullOrEmpty(q.FieldHelpText) && !string.IsNullOrEmpty(q.FieldHelpURL))
            {
                HtmlGenericControl aHelp = new HtmlGenericControl("a");
                aHelp.Attributes.Add("href", q.FieldHelpURL);
                aHelp.Attributes.Add("target", "_blank");
                aHelp.InnerText = q.FieldHelpText;
                pnlTopic.Controls.Add(aHelp);
            }

            pnlIn.Controls.Add(pnlTopic);

            List<QuestionOption> FieldOptions = new List<QuestionOption>();
            
            // load Generic Lists
            FieldOptions = q.FieldOptions;
            
            foreach (QuestionOption qOpt in FieldOptions)
            {
                Panel pnlOPtion = new Panel();

                Type typField = typeof(CheckBox).Assembly.GetType("System.Web.UI.WebControls.CheckBox");
                CheckBox cb = (CheckBox)Activator.CreateInstance(typField);

                cb.ID = qOpt.Name;
                cb.CssClass = q.FieldCSS;
                pnlOPtion.Controls.Add(cb);

                Label lblSuffix = new Label();
                lblSuffix.Text = "&nbsp;&nbsp;" + qOpt.OptionLabel + "&nbsp;&nbsp;";
                pnlOPtion.Controls.Add(lblSuffix);

                if (qOpt.Specials != null)
                {
                    //create control
                    foreach (OptionSpecial os in qOpt.Specials)
                    {
                        switch (os.DisplayType)
                        {
                            case "System.Web.UI.WebControls.TextBox":
                                Type typFieldOpt = typeof (TextBox).Assembly.GetType("System.Web.UI.WebControls.TextBox");
                                TextBox tbOpt = (TextBox) Activator.CreateInstance(typFieldOpt);

                                tbOpt.ID = os.Name;
                                tbOpt.CssClass = q.FieldCSS;
                                pnlOPtion.Controls.Add(tbOpt);
                                break;
                        }
                    }
                }

                pnlIn.Controls.Add(pnlOPtion);

            }

            return pnlIn;
        }

        public Dictionary<Question, NameValueCollection> RetrieveQuestionValue(Question q, ContentPlaceHolder cph)
        {
            Dictionary<Question, NameValueCollection> dFieldValues = new Dictionary<Question, NameValueCollection>();
            NameValueCollection nvc = new NameValueCollection();

            List<QuestionOption> FieldOptions = q.FieldOptions;

            foreach (QuestionOption qOpt in FieldOptions)
            {
                CheckBox cb = (CheckBox)cph.FindControl(qOpt.Name);
                nvc.Add(qOpt.Name, cb.Checked.ToString());
                if ((qOpt.Specials != null) && (cb.Checked))
                {
                    foreach (OptionSpecial os in qOpt.Specials)
                    { 
                        TextBox tb = (TextBox)cph.FindControl(os.Name);
                        nvc.Add(os.Name, tb.Text);
                    }
                }
            }

            dFieldValues.Add(q, nvc);

            return dFieldValues;
        }

        #endregion
    }
}